package nettydemo;/*

 */

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @ClassName NioFileChannel03
 * @Author Final
 * @Project DataStructures
 * 用一个buffer完成文件的读取
 *
 */
public class NioFileChannel03 {

    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream=new FileInputStream("1.txt");
        FileChannel channel = fileInputStream.getChannel();

        FileOutputStream fileOutputStream=new FileOutputStream("2.txt");
        FileChannel channel1 = fileOutputStream.getChannel();
        ByteBuffer allocate = ByteBuffer.allocate(512);
        while (true){
            //重要操作   重置
            //如果没有clean（），就会出现问题，执行flip（），position和limit就会相等，
            allocate.clear();
            int read = channel.read(allocate);

            System.out.println(read);
            if (read==-1){//读完
                break;
            }
            allocate.flip();
            channel1.write(allocate);
        }

        fileInputStream.close();
        fileOutputStream.close();
    }
}
